package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcOldManTypeIetypeMapper;
import com.csun.cmny.provider.model.domain.OmcOldManTypeIetype;
import com.csun.cmny.provider.model.domain.OmcUserType;
import com.csun.cmny.provider.service.OmcOldManTypeIetypeService;
import com.csun.cmny.provider.vo.OmcOldManTypeIetypeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenzheng
 * @description:人员类别减免服务层
 * @date 2019/8/12 14:23
 */
@Slf4j
@Service
@Transactional
public class OmcOldManTypeIetypeServiceImpl extends BaseService<OmcOldManTypeIetype> implements OmcOldManTypeIetypeService {

    @Resource
    private OmcOldManTypeIetypeMapper omcOldManTypeIetypeMapper;

    /**
     * 批量保存或者更新人员类别减免
     *
     * @param list
     * @param loginAuthDto
     * @return
     */
    @Override
    public int batchSaveOmcOldManTypeIetype(List<OmcOldManTypeIetype> list, LoginAuthDto loginAuthDto) {
        int count = 0;
        for (OmcOldManTypeIetype omcOldManTypeIetype : list) {
            omcOldManTypeIetype.setUpdateInfo(loginAuthDto);
            if (omcOldManTypeIetype.isNew()) {//保存
                int n = omcOldManTypeIetypeMapper.insertSelective(omcOldManTypeIetype);
                count += n;
            } else {//更新
                int n = omcOldManTypeIetypeMapper.updateByPrimaryKeySelective(omcOldManTypeIetype);
                count += n;
            }
        }
        return count;
    }

    /**
     * 查询人员类别减免列表
     *
     * @param list
     * @return
     */
    @Override
    public List<OmcOldManTypeIetypeVo> queryOldManTypeIetypeList(List<OmcUserType> list) {
        List<OmcOldManTypeIetypeVo> resultList = new ArrayList<>();
        for (OmcUserType omcUserType: list) {
            OmcOldManTypeIetypeVo omcOldManTypeIetypeVo =new OmcOldManTypeIetypeVo();
            omcOldManTypeIetypeVo.setUserTypeId(omcUserType.getId());
            omcOldManTypeIetypeVo.setUserTypeName(omcUserType.getUserTypeName());
            omcOldManTypeIetypeVo.setList(omcOldManTypeIetypeMapper.selectByUserTypeId(omcUserType));//填充人员类别减免信息
            resultList.add(omcOldManTypeIetypeVo);
        }
        return resultList;
    }

    @Override
    public List<Long> selectUserTypeId(Long groupId) {
        return omcOldManTypeIetypeMapper.selectUserTypeId(groupId);
    }
}
